package com.frankle.mybatisplusgenerator.controller;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import com.frankle.mybatisplusgenerator.App;
import com.frankle.mybatisplusgenerator.constant.StageView;
import com.frankle.mybatisplusgenerator.utils.FontUtils;
import com.frankle.mybatisplusgenerator.utils.I18N;
import com.frankle.mybatisplusgenerator.utils.PropUtil;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.text.Font;

public class OptionsViewController implements Initializable {
	
	@FXML private Label lb_language;
	@FXML private ComboBox<String> language;
	
	@FXML private Label lb_font_size;
	@FXML private ComboBox<Integer> font_size;
	
	@FXML private Label lb_font_family;
	@FXML private ComboBox<String> font_family;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		lb_language.textProperty().bind(I18N.createStringBinding("label.language"));
		language.setItems(FXCollections.observableList(List.of("English", "简体中文", "繁體中文")));
		language.getSelectionModel().select(I18N.getSupportedLocales().indexOf(I18N.getDefaultLocale()));
		
		lb_font_size.textProperty().bind(I18N.createStringBinding("label.font_size"));
		font_size.setItems(FXCollections.observableArrayList(12, 13, 14));
		font_size.getSelectionModel().select(0);
		
		lb_font_family.textProperty().bind(I18N.createStringBinding("label.font_family"));
		font_family.setItems(FXCollections.observableArrayList(Font.getFontNames()));
		font_family.getSelectionModel().select(Font.getDefault().getName());
	}

	/**
	 * 保存
	 * @author Frankle
	 * @date 2020年5月20日
	 */
	@FXML
	public void save() {
		int index = language.getSelectionModel().getSelectedIndex();
		I18N.setLocale(I18N.getSupportedLocales().get(index));
		
		int fontSize = font_size.getSelectionModel().getSelectedItem().intValue();
		String fontFamily = font_family.getSelectionModel().getSelectedItem();
		
		FontUtils.changeFont(fontFamily, fontSize);
		
		PropUtil config = PropUtil.use("config.properties");
		config.set("locale.lang", I18N.getSupportedLocales().get(index).toLanguageTag());
		config.set("font.size", fontSize + "");
		config.set("font.family", fontFamily);
		config.save();
		
		App.STAGE_MAP.get(StageView.OPTIONS_VIEW).close();
	}
}
